package com.bobi.tfca.ui.mainnavigation.home.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import androidx.navigation.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentActivityDetailBinding
import com.bobi.tfca.ui.adapter.ActivityPagerAdapter
import com.bobi.tfca.util.LogUtil
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class ActivityDetailsFragment :
    BaseFragment<FragmentActivityDetailBinding, ActivityDetailsFragmentVM>(
        FragmentActivityDetailBinding::inflate,
        ActivityDetailsFragmentVM::class.java,
        true
    ) {
    // 会员RV
    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout
    private lateinit var pagerAdapter: ActivityPagerAdapter

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentActivityDetailBinding,
        viewModel: ActivityDetailsFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化topPanel
        setTopPanelStyle(binding)

        // 初始化ViewPager
        viewPager = binding.vpActivityDetails
        pagerAdapter = ActivityPagerAdapter(this).apply {
            add { ShowActConditionFragment() }
            add { ShowActDetailsFragment() }
            add { ShowActInfoFragment() }
            add { ManageActMemberFragment() }
        }
        viewPager.adapter = pagerAdapter

        // 初始化tablelayout
        tabLayout = binding.tabLayout
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            when (position) {
                0 -> tab.text = "需知"
                1 -> tab.text = "内容"
                2 -> tab.text = "信息"
                3 -> tab.text = "人员"
            }
        }.attach()

        // 监听头部信息
        publicViewModel!!.activity.observe(viewLifecycleOwner) {
            // 加载图片
            val url = it.outsidePicture
            if (url.isNotBlank() && (!"null".equals(url))) {
                Glide.with(this).load(url).placeholder(R.drawable.defaultbg_h)
                    .into(binding.ivActivityPicture)
            } else {
                binding.ivActivityPicture.setImageResource(R.drawable.defaultbg_h)
            }
            // 标题
            binding.tvActivityName.text = it.activityName
            // 报名人数
            val countText: String = "" + it.signedNum + "/" + it.limitedNum
            binding.tvSignedNum.text = countText
            // 活动状态
            binding.tvActivityStatus.text = it.activityStatus
        }

        // 设置底部按钮
        setBottomButton(binding, viewModel)

        // 获取活动更多信息
        viewModel!!.getActivityDetails(publicViewModel!!, this)
    }

    /**
     * 设置底部按钮
     */
    private fun setBottomButton(
        binding: FragmentActivityDetailBinding,
        viewModel: ActivityDetailsFragmentVM?
    ) {
        publicViewModel!!.activity.observe(this@ActivityDetailsFragment) {
            // 查询用户在当前活动中充当的角色
            val role = publicViewModel!!.activity.value?.role
            LogUtil.i("role----$role")
            if ("成员".equals(role)) {
                // 如果是会员，左边按钮不可见，右边按钮文本为取消报名
                binding.btRight.text = "取消报名"
                binding.btRight.setOnClickListener {
                    // 创建确认框
                    val builder = AlertDialog.Builder(requireContext())
                    builder.setTitle("取消活动")
                    builder.setMessage("你确认取消报名吗")

                    // 设置确认按钮和点击事件
                    builder.setPositiveButton("确认") { dialog, which ->
                        viewModel!!.cancelSign(publicViewModel!!, this)
                    }

                    // 设置取消按钮和点击事件
                    builder.setNegativeButton("取消") { dialog, which ->
                    }

                    // 创建并显示确认框
                    val dialog: AlertDialog = builder.create()
                    dialog.show()
                }
            } else if ("未报名".equals(role)) {
                // 如果是会员，左边按钮不可见，右边按钮文本为报名
                binding.btRight.text = "报名"
                binding.btRight.setOnClickListener {
                    viewModel!!.signUp(publicViewModel!!, this)
                }
            } else if ("负责人".equals(role)) {
                // 如果是活动负责人，左边是结束活动，右边是取消活动
                val activityStatus = publicViewModel!!.activity.value!!.activityStatus
                if ("已结束".equals(activityStatus) || "已取消".equals(activityStatus)) {
                    // 两个按钮都没有
                    binding.btRight.visibility = View.INVISIBLE
                    binding.btLeft.visibility = View.INVISIBLE
                } else if ("未开始".equals(activityStatus)) {
                    // 没有结束按钮，有取消按钮
                    binding.btLeft.visibility = View.INVISIBLE
                    setCancelBtListener(binding, viewModel!!)
                } else if ("进行中".equals(activityStatus)) {
                    setFinishBtListener(binding, viewModel!!)
                    setCancelBtListener(binding, viewModel)
                }
            } else {

            }
        }
    }

    private fun setCancelBtListener(
        binding: FragmentActivityDetailBinding,
        viewModel: ActivityDetailsFragmentVM
    ) {
        binding.btRight.visibility = View.VISIBLE
        binding.btRight.text = "取消活动"
        binding.btRight.setOnClickListener {
            // 创建确认框
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("取消活动")
            builder.setMessage("你确认取消活动吗")

            // 设置确认按钮和点击事件
            builder.setPositiveButton("确认") { dialog, which ->
                viewModel.cancelActivity(publicViewModel!!, this)
            }

            // 设置取消按钮和点击事件
            builder.setNegativeButton("取消") { dialog, which ->
            }

            // 创建并显示确认框
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }
    }

    private fun setFinishBtListener(
        binding: FragmentActivityDetailBinding,
        viewModel: ActivityDetailsFragmentVM
    ) {
        binding.btLeft.visibility = View.VISIBLE
        binding.btLeft.text = "结束活动"
        binding.btLeft.setOnClickListener {
            // 创建确认框
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("结束活动")
            builder.setMessage("你确认结束活动吗")

            // 设置确认按钮和点击事件
            builder.setPositiveButton("确认") { dialog, which ->
                viewModel!!.finishActivity(publicViewModel!!, this)
            }

            // 设置取消按钮和点击事件
            builder.setNegativeButton("取消") { dialog, which ->
            }

            // 创建并显示确认框
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }
    }


    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        binding: FragmentActivityDetailBinding
    ) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        topPanel.rightLayout.visibility = View.GONE
        val title = topPanel.titleText
        title.text = "活动"
    }

}